home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 599 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: in2.uu.net!pipeline!not-for-mail
  2. From: cgister@nyc.pipeline.com (Carey Gister)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help: How to initialize objects created by new class[x]
  5. Date: 5 Jan 1996 04:52:53 -0500
  6. Organization: The Pipeline
  7. Message-ID: <4cisdl$jt3@pipe9.nyc.pipeline.com>
  8. References: <4cicd6$lsv@news.capitalnet.com>
  9. NNTP-Posting-Host: pipe9.nyc.pipeline.com
  10. X-PipeUser: cgister
  11. X-PipeHub: nyc.pipeline.com
  12. X-PipeGCOS: (Carey Gister)
  13. X-Newsreader: The Pipeline v3.4.0
  14.  
  15. On Jan 05, 1996 05:19:34 in article <Help: How to initialize objects
  16. created by new class[x]>, 'wingl@capitalnet.com' wrote: 
  17.  
  18.  
  19. >Let say I have the following class 
  20. >My_class { 
  21. >public: 
  22. >int (int i=0) : a(i), b(i) {} 
  23. >private: 
  24. >int a, b; 
  25. >} 
  26. >For the following statement, default constructor is involved upon  
  27. >the creation of the object. 
  28. >My_class* my_ptr= new My_class; 
  29. >However,  if I use   
  30. >My_class* my_ptr= new My_class[10]; 
  31. >to allocate 10 objects of class My_class, then C++ would not 
  32. >call any constructor to do any initialization.  All the objects 
  33. >allocated are not initializated at all.  
  34. >I wonder what is the proper way to perform object initialization 
  35. >whenever an array of objects are created?  As long as a constructor 
  36. >is involved for each of the object in the array, I don't care if all the  
  37. >objects are initializated the same or not. 
  38. >If there is no way to get constructor to initialize an array of objects 
  39. >upon creation.  Does that means one should not do so? 
  40. >Thanks for any help. 
  41. >Wing 
  42. >wingl@bnr.ca 
  43.  
  44.  
  45. Ahh, but a constructor IS called: the constructor of type MyClass(void)! 
  46. and, in fact, it is 
  47. once for each element of the array. 
  48.  
  49. I too am interested in the answer to your question regarding initialization
  50. of the objects: 
  51. I always call an initialization method after allocating arrays this way. 
  52. The technique is called 
  53. "2-phase construction". 
  54.  
  55. Carey 
  56. [end] 
  57.